/**
 * 剑指 Offer 27. 二叉树的镜像
 * https://leetcode-cn.com/problems/er-cha-shu-de-jing-xiang-lcof/
 */
const { displayTreeNode, createTreeNode } = require('../../libs/public');

/**
 * @param {TreeNode} root
 */
function reverse(root) {
  if (!root) {
    return;
  }
  const temp = root.left;
  root.left = root.right;
  root.right = temp;
  reverse(root.left);
  reverse(root.right);
}

/**
 * @param {TreeNode} root
 * @return {TreeNode}
 */
function mirrorTree(root) {
  reverse(root);
  return root;
}

console.log(displayTreeNode(mirrorTree(createTreeNode([4, 2, 7, 1, 3, 6, 9]))) === '[4,7,2,9,6,3,1]');
